<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>09-状态提升</title>
    <script src="react.development.js" type="text/javascript" charset="UTF-8"></script>
    <script src="react-dom.development.js" type="text/javascript" charset="UTF-8"></script>
    <script src="babel.min.js" type="text/javascript" charset="UTF-8"></script>
</head>
<body>
    <div id="app">

    </div>
</body>
<script type="text/babel">
    //根据摄氏温度判断水是否沸腾
    function BoilingVerdict(props) {
        if(props.celsius >= 100){
            return <h2>水已经沸腾了</h2>
        }
        return <h2>水没沸腾</h2>
    }

    //ReactDOM.render(
    //    <BoilingVerdict celsius="101" />,
    //    document.getElementById("app")
    //);

    //---------------------------------------------------------

    const typeName = {
        c: "摄氏温度",
        f: "华氏温度"
    };
    function TemperatureInput(props) {
        function onTemperatureChange(e) {
            //调用父组件传递给子组件的方法
            props.onTemperatureChange(e.target.value);
        }

        return (
            <fieldset>
                <legend>请输入{typeName[props.type]}</legend>
                {/*onChange={this.onTemperatureChange.bind(this)}是错误写法*/}
                <input type="text" value={props.temperature} onChange={onTemperatureChange} />
            </fieldset>
        );
    }

    //多个组件需要反映相同的变化数据，此时我们建议将共享状态提升到最近的父组件中
    //温度要在<TemperatureInput>、<BoilingVerdict>用到，所以把温度提升到父组件App中，以props的形式传递给子组件
    class App extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                temperature: 0,
                type: "c"
            };
        }

        //子组件调用父组件的方法时传value实参
        onCelsiousChange(value) {
            this.setState({
                temperature: value,
                type: "c"
            });
        }

        //子组件调用父组件的方法时传value实参
        onFahrenheitChange(value) {
            this.setState({
                temperature: value,
                type: "f"
            });
        }

        render() {
            const {temperature,type} = this.state;
            let celsious,fahrenheit;
            //0==''为true，0===''为false
            if(temperature === '') {
                celsious = fahrenheit = '';
            }
            else if(type == "c") {
                celsious = temperature;
                fahrenheit = celsious * 1.8 + 32;
            }
            else {
                fahrenheit = temperature;
                celsious = (fahrenheit - 32)/1.8;
            }

            return (
                <div>
                    <TemperatureInput type="c" temperature={celsious} 
                    onTemperatureChange={this.onCelsiousChange.bind(this)} />
                    <TemperatureInput type="f" temperature={fahrenheit} 
                    onTemperatureChange={this.onFahrenheitChange.bind(this)} />
                    <BoilingVerdict  celsius={celsious} />
                </div>
            );
        }
    }

    ReactDOM.render(
        <App />,
        document.getElementById("app")
    );
</script>
</html>